import storage from 'good-storage'

export const examMixin = {
    data() {
        return {
            examList: {},
            exam: {}
        }
    },
    methods: {
        //  获取所有的考试
        async fetchExams(isCache) {
            if (storage.has("exams") && isCache) return storage.get("exams");
            // let exams = await getExams();

            let exams = [
                {
                    id: 1,
                    title: "全国执业兽医资格考试（基础科目）",
                    start_time: "2022-06-22 00:00:00",
                    end_time: "2030-06-22 00:00:00",
                },
                {
                    id: 2,
                    title: "全国执业兽医资格考试（预防科目）",
                    start_time: "2022-06-22 00:00:00",
                    end_time: "2030-06-22 00:00:00",
                },
                {
                    id: 3,
                    title: "全国执业兽医资格考试（临床科目）",
                    start_time: "2022-06-22 00:00:00",
                    end_time: "2030-06-22 00:00:00",
                },
            ];

            this.examList = exams;
            storage.set("exams", exams);
            return exams;
        },
        //  验证考试所需的参数
        checkExamParams() {
            this.exam = this.getStore("questionsResult");
            //  如果没有获取考试题目,则跳转重新获取考试题目
            if (!this.exam) {
                window.location.href = process.env.ERROR_LOCATION;
            }

            //  验证身份信息,如果没有填写则跳转到登录页面
            this.idCard = this.getStore("idCard");
            this.idNumber = this.getStore("idNumber");
            if (!this.idCard || !this.idNumber) {
                this.$router.push("join");
            }
        },
        setStore(key, value) {
            storage.set(key, value);
        },
        getStore(key) {
            return storage.get(key);
        },
        removeStore(key) {
            storage.remove(key);
        },
        //  根据id获取某一个考试
        getExamById(id) {
            for (let index in this.fetchExams()) {
                let item = this.examList[index];
                if (item.id === id) return item;
            }
            return null;
        },
    }
}
